package cn.iocoder.yudao.module.wine.controller.app.common;


import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.hutool.core.io.FileUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Slf4j
@Tag(name = "微信接口")
@RestController
@RequestMapping("/wine/wechat")
@Validated
public class WXCommonController {

    @Resource
    private WxMaService wxMaService;

    @RequestMapping("/checkMsg")
    public CommonResult<Boolean> check(@RequestBody String msg){
        WxMaSecCheckService secCheckService = wxMaService.getSecCheckService();
        boolean b ;
        try {
            b = secCheckService.checkMessage(msg);
        }catch (WxErrorException e){
            return error(e.getError().getErrorCode(),e.getError().getErrorMsg());
        }
        return success(b);
    }


    @RequestMapping("/checkImg")
    public CommonResult<Boolean> checkImg(@RequestParam("url") String url) throws Exception {
        WxMaSecCheckService secCheckService = wxMaService.getSecCheckService();
        boolean b = secCheckService.checkImage(url);
        return success(b);
    }

    @RequestMapping("/checkVoice")
    public CommonResult<WxMaMediaAsyncCheckResult> checkVoice(@RequestParam("url") String url) throws Exception {
        WxMaSecCheckService secCheckService = wxMaService.getSecCheckService();
        WxMaMediaAsyncCheckResult wxMaMediaAsyncCheckResult = secCheckService.mediaCheckAsync(url, WxMaConstants.SecCheckMediaType.VOICE);
        return success(wxMaMediaAsyncCheckResult);
    }
}

